package com.hpjpw.idoapp.controller;

import com.hpjpw.ido.domain.CheerMoment;
import com.hpjpw.ido.service.ICheerMomentService;
import com.hpjpw.ido.service.ITopicService;
import com.hpjpw.idoapp.service.IDoCheerService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ido/app/cheerMoment")
public class IDoCheerMomentController extends BaseController {
    @Autowired
    ICheerMomentService cheerMomentService;
    @Autowired
    IDoCheerService cheerService;
    @Autowired
    ISysUserService sysUserService;
    @Autowired
    ITopicService topicService;
    /**
     * 查询加油站动态列表
     */
    @ApiOperation("查询加油站动态列表，可带分页参数：pageNum=1&pageSize=10")
    @GetMapping("/list")
    public TableDataInfo list(){
        startPage();
        List<Map<String, Object>> mapList = cheerService.momentListResponse();
        return getDataTable(mapList);
    }
    /**
     * 新增加油站动态
     */
    @ApiOperation("新增加油站动态，接收content(动态内容),picture(图片最多三张),topicId(主题id)")
    @PostMapping()
    public AjaxResult addMoment(@RequestBody CheerMoment cheerMoment) {
        /* 安全校验1，无该发布者 */
        /*if (StringUtils.isNull(sysUserService.selectUserById(cheerMoment.getReportId()))){
            return AjaxResult.error("错误！没有此发布者，请检查reportId！");
        }*/
        /* 安全校验2，无动态内容无图片 */
        if (StringUtils.isNull(cheerMoment.getContent())&&StringUtils.isNull(cheerMoment.getPicture1())&&StringUtils.isNull(cheerMoment.getPicture2())&&StringUtils.isNull(cheerMoment.getPicture3())){
            return AjaxResult.error("错误！没有动态内容和图片！");
        }
        /* 安全校验3，使用主题而主题不存在 */
        if (StringUtils.isNotNull(cheerMoment.getTopicId())&&StringUtils.isNull(topicService.selectTopicById(cheerMoment.getTopicId()))){
            return AjaxResult.error("错误！不存在此主题，请检查topicId");
        }
        Long userId = SecurityUtils.getUserId();
        CheerMoment cheerMomentAdd = new CheerMoment();
        cheerMomentAdd.setReportId(userId);
        cheerMomentAdd.setContent(cheerMoment.getContent());
        cheerMomentAdd.setPicture1(cheerMoment.getPicture1());
        cheerMomentAdd.setPicture2(cheerMoment.getPicture2());
        cheerMomentAdd.setPicture3(cheerMoment.getPicture3());
        cheerMomentAdd.setTopicId(cheerMoment.getTopicId());
        cheerMomentAdd.setTime(new Date());
        cheerMomentService.insertCheerMoment(cheerMomentAdd);
        return AjaxResult.success();
    }

    /**
     * 查询加油站动态
     */
    @ApiOperation("查询加油站动态，接收参数momentId")
    @GetMapping("/{momentId}")
    public TableDataInfo query(@PathVariable("momentId") long momentId){
        List<Map<String, Object>> mapList = new ArrayList<>();
        Map<String, Object> map = cheerService.momentResponse(momentId);
        mapList.add(map);
        return getDataTable(mapList);
    }

    /**
     * 删除加油站动态
     */
    @ApiOperation("删除加油站动态，接收id(动态id)")
    @DeleteMapping()
    public AjaxResult deleteMoment(@RequestBody CheerMoment cheerMoment) {
        CheerMoment cheerMomentCheck = cheerMomentService.selectCheerMomentById(cheerMoment.getId());
        /* 安全校验1，无此动态 */
        if (StringUtils.isNull(cheerMomentCheck)){
            return AjaxResult.error("错误！无此动态，请检查id");
        }
        /* 安全校验2，删除操作者非动态发布者 */
        Long userId = SecurityUtils.getUserId();
        if (!(cheerMomentCheck.getReportId().equals(userId))){
            return AjaxResult.error("错误！没有权限无法删除");
        }
        cheerMomentService.deleteCheerMomentById(cheerMomentCheck.getId());
        return AjaxResult.success();
    }
    //TODO:删除动态时级联删除评论和点赞
}
